package io.renren.modules.app.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.web.util.HtmlUtils;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.itextpdf.text.pdf.BaseFont;

import cn.hutool.core.text.StrFormatter;
import jodd.io.FileUtil;
import jodd.jerry.Jerry;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by lujianing on 2017/5/7.
 */
@Data
@Slf4j
@AllArgsConstructor
public class PdfUtil {
	private File targetFile;
	private String picPath;

	public boolean createPdf(String content) {
		try {
			ITextRenderer render = new ITextRenderer();
			ITextFontResolver fontResolver = render.getFontResolver();
			String font = "simhei.ttf";
			fontResolver.addFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			
			content = HtmlUtils.htmlUnescape(content);
			System.out.println(content);
			content = content.replaceAll("&nbsp;", "").replaceAll("<div></div>", "");
//			String path = this.getClass().getResource("/").getPath()+"/template.html";
//			String readString = FileUtil.readString(new File(path));
			content = StrFormatter.format(mo, content);
			// 解析html生成pdf
			render.setDocumentFromString(content);
			// 解决图片相对路径的问题
			render.getSharedContext().setBaseURL(picPath);

			render.layout();
			render.createPDF(new FileOutputStream(targetFile));			
		}catch(Exception e) {
			log.error("生成pdf异常",e);
			return false;
		}
		return true;
	}
	
	
	private final String mo = "<!DOCTYPE html>\n" + 
			"<html lang=\"en\">\n" + 
			"<head>\n" + 
			"    <meta charset=\"UTF-8\"/>\n" + 
			"    <title>Title</title>\n" + 
			"    <style>\n" + 
			"        body{\n" + 
			"            font-family:SimHei;\n" + 
			"        }\n" + 
			"        .color{\n" + 
			"            color: green;\n" + 
			"        }\n" + 
			"        .pos{\n" + 
			"            position:absolute;\n" + 
			"            left:200px;\n" + 
			"            top:5px;\n" + 
			"            width: 200px;\n" + 
			"            font-size: 10px;\n" + 
			"        }\n" + 
			"    </style>\n" + 
			"</head>\n" + 
			"<body>\n" + 
			"	{}\n" + 
			"</body>\n" + 
			"</html>";
	
	public static void main(String[] args) throws IOException {
		String content = FileUtil.readString(new File("d:/json.json"));
		try {
			ITextRenderer render = new ITextRenderer();
			ITextFontResolver fontResolver = render.getFontResolver();
			String font = "simhei.ttf";
			fontResolver.addFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			content = content.replaceAll("&nbsp;", "");
			System.out.println(content);
			
//			Jerry jerry = Jerry.jerry(content);
//			String html = jerry.html();
//			System.out.println(html);
			
			// 解析html生成pdf
			render.setDocumentFromString(content);

			render.layout();
			
			render.createPDF(new FileOutputStream(new File("c:/a.pdf")));			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
